package com.whf.service.impl;

import com.whf.dto.OrderDTO;
import com.whf.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2018/7/19 0019.
 */
@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {
    @Autowired
    private WxMpService wxMpService;
    //推送订单状态信息
    @Override
    public void pushOrderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
        templateMessage.setTemplateId("pvSE9Pb_T9DNOLP_UTCzqYvaZ2HguF7AhTeVoqIEKjY");
        templateMessage.setToUser("oTcvj01C1kFsQttmwuNCVGPmZO3U");
        List<WxMpTemplateData> data= Arrays.asList(
                new WxMpTemplateData("first","亲,记得查收哦"),
                new WxMpTemplateData("keyword1","微信点餐"),
                new WxMpTemplateData("keyword2","111111111"),
                new WxMpTemplateData("keyword3",orderDTO.getOrderId()),
                new WxMpTemplateData("keyword4",orderDTO.getOrderStausEnum().getMessage()),
                new WxMpTemplateData("keyword5","$"+orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark","欢迎下次惠顾")
        );
        templateMessage.setData(data);

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            log.error("[卖家端微信模板消息推送] 消息发送失败,{}",e);
        }


    }
}
